package dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import beans.News;
import beans.Tag;
@Transactional
public class NewsDAO {

private HibernateTemplate hibernateTemplate;

private Transaction transaction;

	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	public void saveNews(News news){
		hibernateTemplate.save(news);
		transaction = hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction();
		transaction.commit();
	}
	public void addNews(News news){
		news.setDate(new Date());
		news.setLast_update(new Date());
		hibernateTemplate.save(news);
	}
	
	public List<News> GetAllNews(){
		return hibernateTemplate.loadAll(News.class);
	}

	public List<News> DisplayNews(){
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<News> list;
		Query query = session.createQuery("from News order by last_update desc");
		list = (List<News>)query.list();
		for(News n : list){
			initalize(n);
			}
		return list;
		
	}
	
	public void UpdateNews(News news){
		news.setLast_update(new Date());
		hibernateTemplate.update(news);
	}
	
	public void deleteNews(News news){
		hibernateTemplate.delete(news);
	}
	
	public News findNewsById(int news_id){
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query query = session.createQuery("from News where id=:id").setInteger("id", news_id);
		News n = (News) query.uniqueResult();
		initalize(n);
		
		return n;
	}
	
	public List<News> findNewsByUser(String username){
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query q = session.createQuery("from News  n where n.user.username = :username").setString("username", username);
		List<News> x = q.list();
		for(News n : x){
			initalize(n);
			}
		return x;
	}
	
	public List<News> searchName(String queryString){
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println(queryString);
		Query q = session.createQuery("from News where title like '%'|| :queryString ||'%' order by last_update desc").setString("queryString",queryString);
		List<News> list = q.list();
		for(News n : list){
			initalize(n);
			System.out.println(n.getTitle());
			}
		System.out.println(list.size());
		return list;
	}

	public void initalize(News n){
		Hibernate.initialize(n.getUser());
		Hibernate.initialize(n.getTags());
		Hibernate.initialize(n.getComments());
	}
	
	public List<News> searchByTag(String queryString){
		List<News> listA = DisplayNews();
		List<News> listB = new ArrayList<News>(); 
		for(News n : listA){
			initalize(n);
			for(Tag x : n.getTags()){
				if(x.getName().equalsIgnoreCase(queryString)){
					listB.add(n);
					}
			}

			}
		return listB;
	}

	
}
